<template>
  <div class="master-data">
    <el-select
      v-model="checkedData"
      filterable
      remote
      reserve-keyword
      :placeholder="parameter.promptTemplate"
      :remote-method="remoteMethod"
      :loading="loading"
      :disabled="readonly"
      value-key="code"
      clearable
      @change="changeData"
    >
      <el-option
        v-for="item in optionsList"
        :key="item.code"
        :label="
          showFlag ? item.userName + item.fullName : item.code + item.fullName
        "
        :value="item"
      >
      </el-option>
    </el-select>
  </div>
</template>
<script>
/**
 *  http://wiki.zczy56.com/pages/viewpage.action?pageId=50341610  客户
    http://wiki.zczy56.com/pages/viewpage.action?pageId=50346399 内部公司 tms的签约主体， wms的签约主体
    http://172.20.21.211:8000/#/view/m8wJ0eN2                租户下内部用户接口
 */
import masertDataService from '@/services/masertDataService'
import eventBus from '@/assets/utils/eventBus.js'
export default {
  props: {
    detail: {
      type: Object,
      default: () => {
        return {}
      },
    },
    value: [Object, String],
    disabled: Boolean,
    parameter: {
      type: Object,
      default: () => {
        return {}
      },
    },
  },
  data() {
    return {
      checkedData: {},
      loading: false,
      optionsList: [],
      readonly: false,
      showFlag: false,
    }
  },
  created() {
    if (this.value) {
      this.checkedData = JSON.parse(this.value)
      if (this.checkedData.code || this.checkedData.userId) {
        if (
          ['WHQYSJ'].includes(this.detail.templateTypeCode) &&
          (this.parameter.parameterNumber === 'signTarget' ||
            this.parameter.parameterValue.dataType == 1)
        ) {
          this.showFlag = true
        } else {
          this.showFlag = false
        }
        this.optionsList = [this.checkedData]
        this.changeData()
      } else {
        this.remoteMethod()
      }
    } else {
      this.remoteMethod()
    }
    this.readonly = Boolean(this.$route.query.readonly) || this.disabled
    if (
      this.$route.query.contractSource == 1 &&
      this.parameter.parameterNumber === 'signTarget'
    ) {
      this.readonly = true
    }
  },
  methods: {
    remoteMethod(query) {
      //多式联运货主
      let kind = []
      let templateTypeCode = this.detail.templateTypeCode
      let systemFlag = null
      switch (templateTypeCode) {
        case 'TMSHZ': //多式联运货主
          kind = [1, 2]
          systemFlag = 1
          break
        case 'TMSCYS': //多式联运承运商
          kind = [3]
          systemFlag = 1
          break
        case 'TMSSJ': //多式联运个体司机
          kind = [4]
          systemFlag = 1
          break
        case 'TMSCLB': //多式联运车老板
          kind = [5]
          systemFlag = 1
          break
        case 'WMSHZ1': //仓储货主（通用）
          kind = [6]
          systemFlag = 2
          break
        case 'WMSHZ2': //仓储货主（行业）
          kind = [6]
          systemFlag = 2
          break
        case 'YMSQYZK': //园区企业租客
          kind = [33]
          break
        case 'YMSGRZK': //园区个人租客
          kind = [34]
          break
        case 'WHQYSJ': //网货签约司机 个体司机
          kind = [2]
          break
      }
      if (
        this.parameter.parameterNumber === 'signTarget' ||
        this.parameter.parameterValue.dataType == 1
      ) {
        this.remoteSignTarget(query, kind)
      }

      if (
        this.parameter.parameterNumber === 'signSubject' ||
        this.parameter.parameterValue.dataType == 2
      ) {
        this.remoteSignSubject(query, systemFlag)
      }
      if (this.parameter.parameterValue.dataType == 3) {
        this.remoteInternalusers(query)
      }
    },
    remoteSignTarget(query, kind) {
      let templateTypeCode = this.detail.templateTypeCode
      // 对私
      if (['WHQYSJ'].includes(templateTypeCode)) {
        this.showFlag = true
        masertDataService
          .queryUserByUserType({
            userTypes: kind,
            memberName: query,
          })
          .then((res) => {
            this.optionsList = (res.data.memberInfoList || []).map((it) => {
              return {
                ...it,
              }
            })
          })
      } else {
        this.showFlag = false
        // 对公
        masertDataService
          .findSaasAllCustomerList({
            kind: kind, //Tms: 1、货主  2、托运商  3、分供方,4 司机 5车老板(tms); wms: 6 wms货主(wms)
            status: [1],
            codeOrNameLike: query,
            dataPerFlag: 1,
          })
          .then((res) => {
            this.optionsList = (res.data || []).map((it) => {
              return {
                ...it,
              }
            })
          })
      }
    },
    remoteSignSubject(query, systemFlag) {
      this.showFlag = false
      masertDataService
        .findSaasAllCompanyList({
          state: 1,
          // systemFlag: systemFlag, //运输:1.运输,2.仓储,3.园区,4.商贸,5.核算系统,6.金融,7.价格中心,8.网货
          systemFlag: null,
          skipPrdFlag: 1, // 1不限产品范围  其他限制
          codeOrNameLike: query,
          tenantCode: localStorage.getItem('tenantCode'),
        })
        .then((res) => {
          this.optionsList = res.data.map((it) => {
            return {
              ...it,
              code: it.companyCode,
              name: it.companyName,
              fullName: it.companyFullName,
              customerId: it.companyId,
            }
          })
        })
    },
    remoteInternalusers(query) {
      this.showFlag = false
      masertDataService
        .queryInnerUserByTenant({
          pageNum: 1,
          pageSize: 9999,
        })
        .then((res) => {
          this.optionsList = res.data.list.map((it) => {
            return {
              ...it,
              code: it.customerId,
              name: it.customerName,
            }
          })
        })
    },
    changeData() {
      this.$emit('input', String(this.checkedData))
      if (!this.$route.query.readonly) {
        eventBus.$emit('masterChange', this.parameter, this.checkedData)
      }
    },
    getData() {
      return this.checkedData.code || this.checkedData.userId
        ? {
            customerId: this.checkedData.customerId,
            code: this.checkedData.code,
            name: this.checkedData.name,
            fullName: this.checkedData.fullName,
            userId: this.checkedData.userId,
            userName: this.checkedData.userName,
          }
        : null
    },
  },
}
</script>
